//
//  CarViolationViewController.m
//  EasyToo
//
//  Created by linshengqin on 13-9-2.
//VehicleNo":"GDAJ889Q""EnginNo":"3745","VehicleColor":"G","RackNo":"012247

//
//

#import "CarViolationViewController.h"
#import "CarViolationDetailViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "CarViolationModel.h"
#import "DeviceUtil.h"
#import "CarViolationManager.h"
#import "NetworkAlertView.h"
#define SCREEN_HEIGHT [DeviceUtil getScreenHeight]
#define SCREEN_WIDTH [DeviceUtil getScreenWidth]

@interface CarViolationViewController ()

@end

@implementation CarViolationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self.navigationItem setTitle:@"车辆违章"];
  //  carViolation_model = [CarViolationModel sharedata];
    [self initData];

    [self initMainView];
    [self initButton];
    [self initLabel];
    [self initText];

}

-(void)initMainView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self.view addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 700);
    _bgScrollView.showsVerticalScrollIndicator = YES;
    
}

-(void) initButton
{
    blueButton =[UIButton buttonWithType:UIButtonTypeCustom];
    blueButton.frame = CGRectMake(20, 90.0f/480.0f*SCREEN_HEIGHT, 80, 40);
    [blueButton setBackgroundImage:[UIImage imageNamed:@"lanpai1"] forState:UIControlStateNormal];
    [blueButton setBackgroundImage:[UIImage imageNamed:@"lanpai2"] forState:UIControlStateSelected];
    [blueButton setTitle:@"蓝牌" forState:UIControlStateNormal];
    [_bgScrollView addSubview:blueButton];
    [blueButton addTarget:self action:@selector(blueButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    yellowButton =[UIButton buttonWithType:UIButtonTypeCustom];
    yellowButton.frame = CGRectMake(120, 90.0f/480.0f*SCREEN_HEIGHT, 80, 40);
    [yellowButton setBackgroundImage:[UIImage imageNamed:@"huangpai1"] forState:UIControlStateNormal];
    [yellowButton setBackgroundImage:[UIImage imageNamed:@"huangpai2"] forState:UIControlStateSelected];
    [yellowButton setTitle:@"黄牌" forState:UIControlStateNormal];
    [_bgScrollView addSubview:yellowButton];
    [yellowButton addTarget:self action:@selector(yellowButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    blackButton =[UIButton buttonWithType:UIButtonTypeCustom];
    blackButton.frame = CGRectMake(220, 90.0f/480.0f*SCREEN_HEIGHT, 80, 40);
    [blackButton setBackgroundImage:[UIImage imageNamed:@"heipai1"] forState:UIControlStateNormal];
    [blackButton setBackgroundImage:[UIImage imageNamed:@"heipai2"] forState:UIControlStateSelected];
    [blackButton setTitle:@"黑牌" forState:UIControlStateNormal];
    [_bgScrollView addSubview:blackButton];
    [blackButton addTarget:self action:@selector(blackButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //下一步按钮
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButton setFrame:CGRectMake(20, 240.0f/480.0f*SCREEN_HEIGHT, 280, 40)];
    [nextButton setTitle:@"下一步" forState:UIControlStateNormal];        //设置button在没有选中的时候显示的字体
    nextButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];         //设置button显示字体的大小
    [nextButton setBackgroundImage:[UIImage imageNamed:@"button_3"] forState:UIControlStateNormal];    //设置button背景显示图片
    [_bgScrollView addSubview:nextButton];
    [nextButton addTarget:self action:@selector(confirmMessge:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)blueButtonClick:(id)button
{
    [blueButton setSelected:YES];
    [yellowButton setSelected:NO];
    [blackButton setSelected:NO];
    colorType = 1;
}
-(void)yellowButtonClick:(id)button
{
    [blueButton setSelected:NO];
    [yellowButton setSelected:YES];
    [blackButton setSelected:NO];
    colorType = 2;
}
-(void)blackButtonClick:(id)button
{
    [blueButton setSelected:NO];
    [yellowButton setSelected:NO];
    [blackButton setSelected:YES];
    colorType = 3;
}

-(void)confirmMessge:(id)button
{
    
    if ([carNumber.text isEqualToString:@""] == YES || [carFrame.text isEqualToString:@""] == YES || [carEngine.text isEqualToString:@""] == YES || carNumber.text == nil || carFrame.text == nil || carEngine.text == nil)
    {
       UIAlertView *_baseAlert = [[[UIAlertView alloc]  initWithTitle:@"出错" message:@"用户名信息不能为空"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil]autorelease];
        [_baseAlert show];
    }
    else if( [carFrame.text length] != 6  )
    {
        UIAlertView *_baseAlert = [[[UIAlertView alloc]  initWithTitle:@"出错" message:@"请填写6位发动机号"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil]autorelease];
        [_baseAlert show];
    }
    else if( [carEngine.text length] != 4)
    {
        UIAlertView *_baseAlert = [[[UIAlertView alloc]  initWithTitle:@"出错" message:@"请填写4位车架号"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil]autorelease];
        [_baseAlert show];

    }
    else if( !blueButton.selected && !yellowButton.selected && !blackButton.selected)
    {
        UIAlertView *_baseAlert = [[[UIAlertView alloc]  initWithTitle:@"出错" message:@"请选择牌色"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil]autorelease];
        [_baseAlert show];
    }
    else
    {
        [[NetworkAlertView shareView]showNetView];
        
        carViolation_model.vehicleNo =[carNumber text];
        carViolation_model.colorType = [NSString stringWithFormat:@"%d",colorType];
        carViolation_model.rackNo = [carFrame text];
        carViolation_model.enginNo = [carEngine text];
        
        [CarViolationManager shareManager].delegate = self;
        [[CarViolationManager shareManager] searchViolation:carViolation_model.vehicleNo VehicleColor:carViolation_model.colorType RackNo:carViolation_model.rackNo EnginNo:carViolation_model.enginNo ];

    }
}

-(void)initLabel
{
    UIImage *image = [UIImage imageNamed:@"shurubi"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image] ;
    imageView.frame = CGRectMake(20, 20.0f/480.0f*SCREEN_HEIGHT, 10, 15);
    [self.view addSubview:imageView];
    
    UILabel *label_user = [[[UILabel alloc]initWithFrame:CGRectMake(30, 20.0f/480.0f*SCREEN_HEIGHT, 180, 20)]autorelease];
    label_user.text = @"填写和选择车辆信息:";
    [_bgScrollView addSubview:label_user];
}

-(void)initText
{
    carNumber = [[[UITextField alloc] initWithFrame:CGRectMake(30, 40.0f/480.0f*SCREEN_HEIGHT, 250, 40)]autorelease];
    [carNumber setBorderStyle:UITextBorderStyleBezel];
    carNumber.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    carNumber.placeholder = @"车牌号码   粤A";
    carNumber.borderStyle = UITextBorderStyleRoundedRect;
    carNumber.delegate = self;
    carNumber.returnKeyType = UIReturnKeyDone;
    carNumber.layer.borderWidth = 1 ;
    carNumber.layer.cornerRadius = 10 ;
    [_bgScrollView addSubview:carNumber];
    
    carFrame = [[[UITextField alloc] initWithFrame:CGRectMake(30, 140.0f/480.0f*SCREEN_HEIGHT, 250, 40)]autorelease];
    [carFrame setBorderStyle:UITextBorderStyleBezel];
    carFrame.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    carFrame.placeholder = @"车架后6位";
    carFrame.borderStyle = UITextBorderStyleRoundedRect;
    carFrame.delegate = self;
    carFrame.returnKeyType = UIReturnKeyDone;
    carFrame.layer.borderWidth = 1 ;
    carFrame.layer.cornerRadius = 10 ;
    carFrame.tag = 300;
    [_bgScrollView addSubview:carFrame];
    
    carEngine = [[[UITextField alloc] initWithFrame:CGRectMake(30, 190.0f/480.0f*SCREEN_HEIGHT, 250, 40)]autorelease];
    [carEngine setBorderStyle:UITextBorderStyleBezel];
    carEngine.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    carEngine.placeholder = @"发动机后4位";
    carEngine.borderStyle = UITextBorderStyleRoundedRect;
    carEngine.delegate = self;
    carEngine.returnKeyType = UIReturnKeyDone;
    carEngine.layer.borderWidth = 1 ;
    carEngine.layer.cornerRadius = 10 ;
    carEngine.tag = 301;
    [_bgScrollView addSubview:carEngine];
}

-(void)dealloc
{
    [CarViolationManager shareManager].delegate = nil;
    [super dealloc];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.view.frame.size.width;
    float height = self.view.frame.size.height;
    if(offset > 0)
    {
        CGRect rect = CGRectMake(0.0f, -offset,width,height);
        self.view.frame = rect;
    }
    [UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
    self.view.frame = rect;
    [UIView commitAnimations];
    [textField resignFirstResponder];
    return YES;
}

-(void) initData
{
    carViolation_model = [CarViolationModel sharedata];
    
    
}

- (void)onSearchCarViolationFinish:(BOOL)isSuccess
                         Violation:(NSMutableArray *)violationInfoArray
                           PostFee:(NSString *)postFee
{
    if(isSuccess)
    {
        [[NetworkAlertView shareView]delView];
        carViolation_model.postFee = postFee;
        carViolation_model.violationInfoArray = violationInfoArray;
        CarViolationDetailViewController *detail = [[[CarViolationDetailViewController alloc]init]autorelease];
        [self.navigationController pushViewController:detail animated:YES];

    }
    else
    {
        printf("111111\n");
        [[NetworkAlertView shareView]delView];
        baseAlert = [[[UIAlertView alloc] initWithTitle:@"系统繁忙或无此车牌信息" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil] autorelease];
        [baseAlert show];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqualToString:@"\n"]){
        return YES;
    }
    
    NSString * aString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (textField.tag == 300)
    {
        if ([aString length] > 6) {
            textField.text = [aString substringToIndex:6];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                            message:@"请输入6位数"
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
            return NO;
        }
    }
    else if(textField.tag == 301 )
    {
        if ([aString length] > 4)
        {
            textField.text = [aString substringToIndex:4];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                            message:@"请输入4位数"
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
            return NO;
        }
    }
    return YES;
    
}

@end
